﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace KBC_Quiz_Show.Bussiness
{
    class QuestionBus
    {
        private int count = 0;
        private QuesstionList qList;// danh sach chua 13 cau hoi
        private QuesstionList[] q;//chua tap cac cau hoi cung muc level
        private String[] ansTrue; //chua tap dap an dung

        public QuestionBus()
        {
            qList = new QuesstionList();
            q = new QuesstionList[13];
            count = 0;
            for (int i = 0; i < 13; i++)
            {
                q[i] = new QuesstionList();
            }
        }

        public int getCount()
        {
            return count;
        }

        public String[] getAnstrue()
        {           
            int n = qList.getN();
            ansTrue = new string[n];
            for (int i = 0; i < n; i++)
            {
                ansTrue[i] = qList.getQues(i).Anstrue;
            }
            return ansTrue;
        }

        public void setCount(int count)
        {
            this.count = count;
        }

        public QuesstionList getQ(int k)
        {
            int i = 0;
            while (i < 13)
            {
                if (i == k)
                {                    
                    break;
                }
                i++;
            }
            return q[i];
        }

        public QuesstionList getQlist()
        {
            return qList;
        }

        public void setQ(QuesstionList q, int k)
        {
            int i = 0;
            while (i < 13)
            {
                if (i == k)
                {
                    this.q[i] = q;
                    break;
                }
                i++;
            }
        }

        public void randomQues()
        {
            
            for (int i = 0; i < 13; i++)
            {
                q[i].getQuesByLevel(i+1);
            }

            int indexRandom;
            Random rd = new Random();
            
            for (int i = 0; i < 13; i++)
            {
                if (q[i].getN() > 0)
                {
                    indexRandom = rd.Next(0, q[i].getN());
                    qList.addQues(q[i].getQues(indexRandom));
                    q[i].removeQues(indexRandom);
                }
                else
                {
                    qList.addQues(q[i].getQues(0));
                }
            }
        }

        public void changeLable(Label lb)
        {
            lb.ForeColor = Color.Red;
        }

        public void choiceFifty(RadioButton[] arrRadio, String ans)
        {
            int x = 0, y = 1;
            int index = 0;
            for (int i = 0; i < 4; i++)
            {
                if (ans.CompareTo(arrRadio[i].Text.ToString()) == 0)
                {
                    index = i;
                    break;
                }
            }
            Random rd = new Random();
            do
            {
                x = rd.Next(0, 3);
            }
            while (x == index);
            do
            {
                y = rd.Next(0, 3);
            }
            while (y == index && x == y);
            //bo di 2 phuong an sai
            arrRadio[x].Enabled = false;
            arrRadio[y].Enabled = false;
        }

        //ham kiem tra dap an dung
        public bool checkAnswer(String[] ans, int index, RadioButton[] rd)
        {
            bool flag = false;
            for (int i = 0; i < 4; i++)
            {
                String str = "";
                if (rd[i].Checked)
                {
                    str = rd[i].Text.ToString();
                }
                if (ans[index].CompareTo(str) == 0)
                {
                    flag = true;
                }
            }
            return flag;
        }

        public void choiceFlip(int i)
        {
            Random rd = new Random();
            int n = q[i].getN();
            int k = rd.Next(0, n);
            for (int j = 0; j < 13; j++)
            {
                if (j == i)
                {
                    qList.removeQues(i);
                }
            }
            qList.addQues(q[i].getQues(k), i);
            q[i].removeQues(k);
        }

        public void DoubleAns()
        {

        }
    }
}
